// 登录模块
import Lodash from 'lodash'
import http from '../service/myHttp'
import urls from '../service/url'
 const message_module = {
    state: {
        newMessageStatistics: {
            //新消息统计
            suggestions: 0, //我的建言 新回复数
            consult: 0 //我的咨询 新回复数
        },
        timer: null //定时器 id
    },
    mutations: {
        setNewMessageStatistics(state, data) {
            //设置新消息统计
            Lodash.merge(state.newMessageStatistics, data)
                // state.userinfo = data
        },
        setTimer(state, data) {
            //设置定时器 id
            state.timer = data
        }
    },
    getters: {},
    actions: {
        getNewMessageStatistics(context) {
            //获取新消息统计
            let userinfo = context.rootState.login_module.userinfo

            http.get(urls.HQWDHFQk, {
                consultor: userinfo.wxId
            }).then(res => {
                let data = res.data.data
                    //console.info('获取新消息统计', data)
                context.commit('setNewMessageStatistics', data)

            })
        },
        startNewMessageTimer(context) {
            //开始新消息定时器
            //console.info('开始新消息定时器')
            context.dispatch('getNewMessageStatistics')
            let timer = setInterval(() => {
                context.dispatch('getNewMessageStatistics')
            }, 30000)
            context.commit('setTimer', timer)
        },
        stopNewMessageTimer(context) {
            //停止当前消息定时器
            //console.info('停止新消息定时器')
            if (context.state.timer) {
                clearInterval(context.state.timer)
            }
            context.commit('setTimer', null)
        }

    }
}

export default message_module